from ..repositories import budget_repository

async def get_budgets(user_id):
    """
    获取用户预算列表
    :param user_id: 用户ID
    :return: 包含预算列表的字典
    """
    try:
        result = await budget_repository.get_budgets(user_id)
        
        if result is None:
            return {"code": 1, "message": "获取预算列表失败"}
            
        return {
            "code": 0,
            "data": result
        }
    except Exception as e:
        print(f"获取预算列表失败: {str(e)}")
        return {"code": 1, "message": f"获取预算列表失败: {str(e)}"}

async def validate_budget_data(category, amount):
    """
    验证预算数据
    :param category: 预算类别
    :param amount: 预算金额
    :return: (bool, str) - (验证是否通过, 错误信息)
    """
    if not category or not category.strip():
        return False, "请输入预算类别"
    if amount <= 0:
        return False, "金额必须大于0"
    return True, ""

async def add_budget(user_id, category, amount, description):
    """
    添加新预算
    :param user_id: 用户ID
    :param category: 预算类别
    :param amount: 预算金额
    :param description: 预算描述
    :return: 操作结果
    """
    # 首先验证输入数据
    is_valid, error_message = await validate_budget_data(category, amount)
    if not is_valid:
        return {"code": 1000, "message": error_message}

    try:
        result = await budget_repository.add_budget(user_id, category, amount, description)
        
        if result is None:
            return {"code": 1, "message": "添加预算失败"}
            
        return {
            "code": 0,
            "message": "添加预算成功"
        }
    except Exception as e:
        print(f"添加预算失败: {str(e)}")
        return {"code": 1, "message": f"添加预算失败: {str(e)}"}

async def update_budget(user_id, budget_id, category, amount, description):
    """
    更新预算信息
    :param user_id: 用户ID
    :param budget_id: 预算ID
    :param category: 预算类别
    :param amount: 预算金额
    :param description: 预算描述
    :return: 操作结果
    """
    # 首先验证输入数据
    is_valid, error_message = await validate_budget_data(category, amount)
    if not is_valid:
        return {"code": 1000, "message": error_message}

    try:
        result = await budget_repository.update_budget(user_id, budget_id, category, amount, description)
        
        if not result:
            return {"code": 1, "message": "更新预算失败"}
            
        return {
            "code": 0,
            "message": "更新预算成功"
        }
    except Exception as e:
        print(f"更新预算失败: {str(e)}")
        return {"code": 1, "message": f"更新预算失败: {str(e)}"}

async def delete_budget(user_id, budget_id):
    """
    删除预算
    :param user_id: 用户ID
    :param budget_id: 预算ID
    :return: 操作结果
    """
    try:
        result = await budget_repository.delete_budget(user_id, budget_id)
        
        if not result:
            return {"code": 1, "message": "删除预算失败"}
            
        return {
            "code": 0,
            "message": "删除预算成功"
        }
    except Exception as e:
        print(f"删除预算失败: {str(e)}")
        return {"code": 1, "message": f"删除预算失败: {str(e)}"}

async def update_budget_order(user_id, budget_orders):
    """
    更新预算排序
    :param user_id: 用户ID
    :param budget_orders: 包含预算ID和排序值的列表 [{"id": 1, "sort_order": 0}, ...]
    :return: 操作结果
    """
    try:
        result = await budget_repository.update_budget_order(user_id, budget_orders)
        if not result:
            return {"code": 1, "message": "更新排序失败"}
        return {
            "code": 0,
            "message": "更新排序成功"
        }
    except Exception as e:
        return {"code": 1, "message": f"更新排序失败: {str(e)}"} 